package com.asset.service;

import com.asset.dao.ApprovalPhaseDao;
import com.asset.dao.ApprovalProcessDao;
import com.asset.dao.ApprovalRecordDao;
import com.asset.dao.PurchaseDao;
import com.asset.entity.Purchase;
import com.asset.entity.approval.Phase;
import com.asset.entity.approval.Process;
import com.asset.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApprovalService {
    @Autowired
    private ApprovalProcessDao approvalProcessDao;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private ApprovalPhaseDao approvalPhaseDao;

    @Autowired
    private ApprovalRecordDao approvalRecordDao;

    //根据参数判断流程并返回总流程
    public Process getProcessPurchase(Purchase purchase) {
        //先解析购置表编号的前两位，当成审批类型比如gz
        String type = purchase.getNumber().substring(0, 2);
        return approvalProcessDao.getProcessPurchase(purchase,type);
    }

    //添加购置流程阶段
    public void addPhasePurchase(Purchase purchase, Process process) {
        //获取流程的第一个申请id
        String[] ids = process.getProcess().split(",");
        String present = ids[0];
        approvalPhaseDao.addPhasePurchase(purchase,process,present);
    }

    //添加购置审批记录表
    public void addRecordPurchase(Purchase purchase, Phase phase) {
        //生成redis的前缀
        redisUtils.setValue(new StringBuilder("jl"));
        String number = redisUtils.createNumber();

        approvalRecordDao.addRecordPurchase(purchase,phase,number);
    }
}
